﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.FrameXml.Components.Support;
using Microsoft.WowAddonStudio.FrameXml.Serialization;
using Microsoft.WowAddonStudio.FrameXml.Services;

namespace Microsoft.WowAddonStudio.FrameXml.Components.Editors
{
	/// <summary>
	/// 
	/// </summary>
	public partial class BackdropEditorForm : Form
	{
		private readonly Rectangle drawRectangle;
		private readonly FrameXmlGraphics frameXmlGraphics;

		/// <summary>
        /// Initializes a new instance of the <see cref="BackdropEditorForm"/> class.
		/// </summary>
		/// <param name="backdropObject">The backdrop object.</param>
		/// <param name="provider">The provider.</param>
		public BackdropEditorForm(Backdrop backdropObject, IServiceProvider provider)
		{
			InitializeComponent();

			BackdropPropertyGrid.SetServiceProvider(provider);

			frameXmlGraphics = new FrameXmlGraphics(
				(FrameXmlImageService) provider.GetService(typeof (FrameXmlImageService)),
				(FrameXmlFontService) provider.GetService(typeof (FrameXmlFontService)),
				PreviewPanelSurface.CreateGraphics());

			BackdropObject = backdropObject;
			BackdropPropertyGrid.PropertyValueChanged += OnPropertyValueChanged;
			PreviewPanelSurface.Paint += PreviewPanelSurfacePaint;
			drawRectangle = new Rectangle(20, 20, PreviewPanelSurface.Width - 40, PreviewPanelSurface.Height - 40);
		}

		/// <summary>
		/// Gets or sets the backdrop object.
		/// </summary>
		/// <value>The backdrop object.</value>
		public Backdrop BackdropObject
		{
			get { return BackdropPropertyGrid.SelectedObject as Backdrop; }
			set { BackdropPropertyGrid.SelectedObject = value; }
		}


		/// <summary>
		/// Handles the Click event of the ButtonOk control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void ButtonOk_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.OK;
			Close();
		}

		/// <summary>
		/// Handles the Click event of the ButtonCancel control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void ButtonCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			Close();
		}

		/// <summary>
		/// Called when [property value changed].
		/// </summary>
		/// <param name="s">The s.</param>
		/// <param name="e">The <see cref="System.Windows.Forms.PropertyValueChangedEventArgs"/> instance containing the event data.</param>
		private void OnPropertyValueChanged(object s, PropertyValueChangedEventArgs e)
		{
			PreviewPanelSurface.Invalidate();
		}


		/// <summary>
		/// Draws the backdrop.
		/// </summary>
		/// <param name="backdrop">The backdrop.</param>
		private void DrawBackdrop(Backdrop backdrop)
		{
			frameXmlGraphics.DrawBackdrop(backdrop, drawRectangle);
		}

		/// <summary>
		/// Pres the view panel surface paint.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.Forms.PaintEventArgs"/> instance containing the event data.</param>
		private void PreviewPanelSurfacePaint(object sender, PaintEventArgs e)
		{
			DrawBackdrop(BackdropObject);
		}
	}
}